package amani.wechat.platform.services.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import amani.wechat.platform.services.ICommonService;
import amani.wechat.platform.services.IOrdersService;
import amani.wechat.platform.services.model.CompanyInfoEx;
import amani.wechat.platform.services.model.HotProduct;
import amani.wechat.platform.services.model.Staffinfo;
import amani.wechat.platform.utils.MathUtils;
import amani.wechat.platform.webserviceClient.Companyinfo;

@Service("OrdersService")
public class OrdersService implements IOrdersService{
	
	@Resource(name="CommonService")
	ICommonService commonService ;

	@Override
	public List<String> getOdersFactProject(String openId) {  
		List<String>  reusult=new ArrayList<String>();
		String jsonString=commonService.loadOrders(openId);
		net.sf.json.JSONArray array= net.sf.json.JSONArray.fromObject(jsonString);  
		for (int i = 0; i < array.size(); i++) {
			net.sf.json.JSONObject object=	(net.sf.json.JSONObject)array.get(i);
			String value=(String)object.get("orderfactproject");
			if(value!=null && !value.isEmpty()){
				if(!reusult.contains(value)){
					reusult.add(value);
				}
			}
		}
		return reusult;
	}

	@Override
	public boolean saveOrder(String strCompId, String strBillId,
			String strCardNo, String orderPhone, String strDate,
			String strTime, String openid, int billType, String complydetail) { 
		return commonService.saveOrders(strCompId, strBillId, strCardNo, orderPhone, strDate, strTime, openid, billType, complydetail);
		
	}
	@Override
	public List<HotProduct> getHotProduct(){
	    List<Map<String,Object>> mapList=	commonService.loadHotProducts();
	    List<HotProduct> list=new ArrayList<HotProduct>();
	    for(int i=0;i<mapList.size();i++){
	    	Map<String,Object> temp=mapList.get(i);
	    	HotProduct item=new HotProduct();
	    	item.setId((Integer)temp.get("id"));
	    	item.setGroupname((String)temp.get("groupname"));
	    	item.settype((Integer )temp.get("type"));
	    	item.setName((String)temp.get("name"));
	    	item.setCid((Integer)temp.get("cid"));
	    	item.setPrice((Double)temp.get("price"));
	    	item.setOriginalcost((Double)temp.get("originalcost"));
	    	item.setCardId((String)temp.get("cardId"));
	    	item.setContent((String)temp.get("content"));
	    	item.setIshot((Boolean)temp.get("ishot"));
	    	item.setIntro((String)temp.get("intro"));
	    	item.setMainimageurl((String)temp.get("mainimageurl"));
	    	item.setSort((Integer)temp.get("sort"));
	    	item.setSumnum((Integer)temp.get("sumnum"));
	    	item.setIsopen((Boolean)temp.get("isopen"));
	    	item.setMinute((Integer)temp.get("minute"));
	    	list.add(item);
	    }
	    return list;
	}
	@Override
	public List<Staffinfo> getConsumedStaffinfos(String openid,String projecttype){
		   List<Map<String,Object>> mapList=	commonService.loadConsumedStaffinfos(openid,projecttype); 
		    List<Staffinfo> list=new ArrayList<Staffinfo>();
		    for(int i=0;i<mapList.size();i++){
		    	Map<String,Object> temp=mapList.get(i);
		    	Staffinfo item=new Staffinfo();  
		    	item.setStaffno((String)temp.get("staffno"));
		    	item.setStaffopenid((String)temp.get("staffopenid"));
		    	item.setStaffimageurl((String)temp.get("staffimageurl"));
		    	item.setStaffname((String)temp.get("staffname"));
		    	item.setDescription((String)temp.get("description"));
		    	item.setCompno((String)temp.get("compno"));
		    	item.setCompname((String)temp.get("compname"));
		    	item.setXcoordinate((String)temp.get("xcoordinate"));
		    	item.setYcoordinate((String)temp.get("ycoordinate"));
		    	item.setStarnum((Integer)temp.get("starnum"));
		       	item.setPrice(Double.parseDouble(temp.get("price").toString()));
		    	item.setCardId((String)temp.get("cardId"));
		    	list.add(item);
		    }
		    return list;
	}
	@Override
	public List<Staffinfo> getNearStaffinfos(String projecttype,String xcoordinate,String ycoordinate){
		List<Companyinfo> companyInfos= commonService.loadAllCompanyinfo();

		if(companyInfos.size()>0){
			List<CompanyInfoEx> comanyInfoExs=new ArrayList<CompanyInfoEx>();
			for(int i=0;i<companyInfos.size();i++){
				CompanyInfoEx item=	 new CompanyInfoEx(companyInfos.get(i));
				if( item.getXcoordinate()!=null &&  item.getYcoordinate()!=null){
					try{
						Double x1=	Double.parseDouble( xcoordinate);
						Double y1=	Double.parseDouble( ycoordinate);
						Double x2=	Double.parseDouble( item.getXcoordinate());
						Double y2=	Double.parseDouble(  item.getYcoordinate());
						Double meter=MathUtils.Distance(x1,y1,x2,y2); 
						 item.setMeters(meter);
					}catch(NumberFormatException  e){ 
						 item.setMeters(Double.MAX_VALUE);
					}
				}else{
					 item.setMeters(Double.MAX_VALUE);
				}
				comanyInfoExs.add(item);
			}
			Collections.sort(comanyInfoExs);
			for(CompanyInfoEx tmp:comanyInfoExs){
				System.out.println(tmp.getCompname()+"|"+tmp.getMeters());
			}
			List<Map<String,Object>> mapList=commonService.loadStaffinfosByCompno(comanyInfoExs.get(0).getCompno(), projecttype, 5); 
		    List<Staffinfo> list=new ArrayList<Staffinfo>();
		   
		    for(int i=0;i<mapList.size();i++){
		    	Map<String,Object> temp=mapList.get(i);
		    	Staffinfo item=new Staffinfo();  
		    	item.setStaffno((String)temp.get("staffno"));
		    	//System.out.println(temp.get("staffopenid"));
		    	item.setStaffopenid((String)temp.get("staffopenid"));
		    	item.setStaffimageurl((String)temp.get("staffimageurl"));
		    	item.setStaffname((String)temp.get("staffname"));
		    	item.setDescription((String)temp.get("description"));
		    	item.setCompno((String)temp.get("compno"));
		    	item.setCompname((String)temp.get("compname"));
		    	item.setXcoordinate((String)temp.get("xcoordinate"));
		    	item.setYcoordinate((String)temp.get("ycoordinate"));
		    	item.setStarnum((Integer)temp.get("starnum"));
		    	item.setPrice(Double.parseDouble(temp.get("price").toString()));
		    	item.setCardId((String)temp.get("cardId"));
		    	item.setMobilephone((String)temp.get("mobilephone"));
		    	item.setCompaddress((String)temp.get("compaddress"));
		    	list.add(item);
		    }
		    return list;
		}else{
			return new ArrayList<Staffinfo>();
		}
		
	}
}
